<?php

	$action = fgsBase::getParam(0);
	$id = (int)fgsBase::getParam(1);
	$form = null;

	// ---------------------------------------------------------------
	// ADD or EDIT a proxy 
	// ---------------------------------------------------------------
	if ($action == 'add' || $action == 'edit') {
		$form = new htmlForm('proxy');
		
		if ($form->isCancel())
			fgsBase::redirect('admin_proxys');
		
		if ($action == 'add') {
			$form->addSection(tr('Add a proxy'));
		} else {
			$proxy = fgsProxys::getById($id);
			if (!$proxy)
				fgsBase::redirect('admin_proxys');
			$form->addSection(sprintf(tr('Edit proxy #%d'), $id));

			$form->default_values['key'] = $proxy['fgsd_key'];
			$form->default_values['name'] = $proxy['fgsd_name'];
			$form->default_values['ip'] = $proxy['fgsd_ip'];
			$form->default_values['status'] = $proxy['fgsd_status'];
			$form->default_values['bl'] = $proxy['fgsd_isblacklisted'];
			$form->default_values['owner_id'] = $proxy['owner_id'];
			$form->default_values['owner_name'] = $proxy['owner_name'];
		}

		$form->addField(tr('Name'), 'name', 'text', true, 'text255');
		$form->addField(tr('Key'), 'key', 'text', true, 'md5');
		$form->addField(tr('Password'), 'password', 'text');
		$form->addField(tr('Ip'), 'ip', 'text');
		$form->addField(tr('Owner (ID)'), 'owner_id', 'text');
		$form->addField(tr('Owner (Name)'), 'owner_name', 'text');
		$form->addField(tr('Online'), 'status', 'checkbox');
		$form->addField(tr('Blacklisted'), 'bl', 'checkbox');
		$form->addSubmit(tr('Submit'));

		// add proxy if post is good
		if ($form->isPost() && $form->isValid()) {
			$name = $form->get('name');
			$key = $form->get('key');
			$password = $form->get('password');
			$ip = $form->get('ip');
			$owner_id = $form->get('owner_id');
			$owner_name = $form->get('owner_name');
			$status = $form->get('status') ? true : false;
			$blacklisted = $form->get('bl') ? true : false;

			if (!empty($password))
				$password = md5($password);
			else
				$password = $proxy['fgsd_pwd'];
			
			if ($action == 'add')
				fgsProxys::add($name, $key, $password, $ip, $owner_id, $owner_name, $status, $blacklisted);
			else 
				fgsProxys::update($id, $name, $key, $password, $ip, $owner_id, $owner_name, $status, $blacklisted);

			fgsBase::redirect('admin_proxys');
		}


	// ---------------------------------------------------------------
	// SHOW a proxy
	// ---------------------------------------------------------------
	} else if ($action == 'show') {
		fgsProxys::setBlacklisted($id, 0);
		fgsBase::redirect('admin_proxys');
		
	// ---------------------------------------------------------------
	// HIDE a proxy
	// ---------------------------------------------------------------
	} else if ($action == 'hide') {
		fgsProxys::setBlacklisted($id, 1);
		fgsBase::redirect('admin_proxys');

	}
	
?>
